home *** CD-ROM | disk | FTP | other *** search
- Path: solon.com!not-for-mail
- From: mshan@ibm.net (Mike Shannon)
- Newsgroups: comp.lang.c,comp.lang.c.moderated,hp.unix,comp.sys.hp.apps,comp.sys.hp.hpux
- Subject: Re: C coding problem
- Date: 10 Apr 1996 07:11:01 -0500
- Organization: Digital Solutions
- Sender: clc@solutions.solon.com
- Approved: clc@solutions.solon.com
- Message-ID: <4kg8gl$iht@solutions.solon.com>
- References: <4ianbf$h86@solutions.solon.com> <4iemcl$a05@solutions.solon.com> <4io1io$no4@solutions.solon.com> <4j06na$808@solutions.solon.com> <4jttan$3gf@solutions.solon.com>
- Reply-To: mshan@ibm.net (Mike Shannon)
- NNTP-Posting-Host: solutions.solon.com
- X-Newsreader: IBM NewsReader/2 v1.2
-
- In <4jttan$3gf@solutions.solon.com>, nsmart@indigo.ie (Niall Smart) writes:
- >
- >>: I recently wrote a loop that went like this:
- >
- >>: while (p < end_p)
- >>: ++*p++;
- >
- >>: Maybe some will find it a counter-example; I think it's good
- >>: code that embodies the way C is designed to work. Arrays are
- >>: second rate objects in C. Use pointers.
- >
- >That code scores negative for maintainability, why use arcane
- >features of the language that can be replaced with more readable code?
- >If you read any of the books on writing good code you will find
- >hundreds of reasons to avoid code like that. The execution time you
- >*might* be saving is offset many times by the time it takes a
- >maintainer to figure it out.
- >
- >Secondly, why not use arrays? In many places they are easier to
- >understand - I for one am not going to compromise the readability
- >of my programs because of some theoretical bull about them
- >being second rate objects. (whatever that is intended to mean)
- >a[i] is converted to *(a + i) by the compiler in any case so
- >whats the advantage with your approach?
- >
- >Niall
-
- Sure, a[i] is the same as *(a+i). Both involve keeping up with
- two variables, and, when i incremented, require a multiplication
- of i times sizeof(*a) to add to a to find the address of the next
- row.
-
- But the example above is doing neither. p++ involves only one
- variable, and only addition of sizeof(*p) to find the address of
- the next row. Notice especially that there is no multiplication
- required. Agreed the per event difference is small, but if a
- program does a large number of array sweeps, the small
- advantage adds up. Some optimizers may do this for you.
-
- I agree with simple syntax, though, and expect that something
- like:
-
- while (p < end_p)
- {
- ++(*p);
- p++;
- }
-
- would produce the same assembly output as the above example,
- and pose less interpretation risk in the future.
-
- Mike Shannon
- Houston, Texas
-
- <!> Once we had computers and dumb terminals.
- <!> Now we have computers and stupid terminals.
- <!> Thanks, Microsoft.
-